package com.softcomputer.rpt.ejb;

import com.softcomputer.rpt.entity.ExCourse;
import com.softcomputer.rpt.entity.LogMessage;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class ExCourseSession {

    @PersistenceContext(unitName = "Exchanger")
    private EntityManager em;
    private List<BigDecimal> values;
    private List<String> currency;

    public List<ExCourse> retrieve() {
        return em.createNamedQuery("ExCourse.findAll").getResultList();
    }

    public List<LogMessage> messagesRetrieve() {
        Query getAllMessagesQuery = em.createNamedQuery("LogMessage.findAll");
        return getAllMessagesQuery.getResultList();
    }

    public List<BigDecimal> getValues() {
        values = new ArrayList<BigDecimal>();
        List<ExCourse> courses = em.createNamedQuery("ExCourse.findAll").getResultList();
        for (ExCourse exCourse : courses) {
            values.add(new BigDecimal(exCourse.getIndollars()));
        }
        return values;
    }

    public List<String> getCurrency() {
        currency = new ArrayList<String>();
        List<ExCourse> courses = em.createNamedQuery("ExCourse.findAll").getResultList();
        for (ExCourse exCourse : courses) {
            currency.add(exCourse.getCurrency());
        }
        return currency;
    }

    public BigDecimal getExchangeRate(String fromCurrency, String toCurrency) {
        List<ExCourse> from = em.createNamedQuery("ExCourse.findByCurrency").setParameter("currency", fromCurrency).getResultList();
        List<ExCourse> to = em.createNamedQuery("ExCourse.findByCurrency").setParameter("currency", toCurrency).getResultList();
        return new BigDecimal(to.get(0).getIndollars() / from.get(0).getIndollars());
    }
}
